﻿package shapes.shapes2D.distribution
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Point;
	/**
	 * http://weblogs.java.net/blog/malenkov/archive/2009/02/triangular_tile.html
	 * https://malenkov.dev.java.net/20090225/triangle.java
	 * @author Nicolas Barradeau
	 */
	public class Triangular extends Sprite
	{
		
		static private var S:Number = 40;
		static private var H:Number = S * 1.5;
		static private var R:Number = S * Math.cos(Math.PI / 6.0);
		static private var W:Number = R * 2;
		static private var points:Vector.<Point>;
		
		public function Triangular()
		{
		}
			
		static public function distribute( size:Number = 25, width:Number = 250, height:Number = 250, jitter:Number = 0 ) : Vector.<Point>
		{
			
			S = size;
			H = S * 1.5;
			R = S * Math.cos(Math.PI / 6.0);
			W = R * 2;
				
			width += R;
			height += H;
			
			points = new Vector.<Point>();
			var i:int = 0;
			var j:int = 0;
			
			var y:Number = 0.0;
			while (y < height) 
			{
				
				var x:Number = i * R - R * j;
				while (x < width) 
				{
					var up:Boolean = (i & 1) == 1;
					points.push( 	new Point( (x - R), (up ? y : y + H) ),
									new Point(  x, 		(up ? y + H : y) ),
									new Point( (x + R), (up ? y : y + H) ) );
					
					x += R;
					i++;
				}
				y += H;
				j++;
				i = j - 1;
			}
			
			fRemoveDup( points );
			
			if( jitter != 0 ) jitterPoints( jitter );
			
			return points;
			
		}
		
		static private function fRemoveDup( ac:Vector.<Point>) : void
		{
			var i:int, j : int;
			for (i = 0; i < ac.length - 1; i++)
				for (j = 0; j < ac.length; j++)
					if ( ac[i].equals( ac[j] ) )
					ac.splice(j, 1);
		}
		
		static private function jitterPoints( radius:Number ):void
		{
			var rad:Number;
			var angle:Number;
			for each( var p:Point in points )
			{
				
				rad = radius * Math.random();
				angle = ( 2 * Math.PI ) * Math.random();
				p.x += rad * Math.cos(angle);
				p.y += rad * Math.sin(angle);
				
			}
		}
		
		
	}

}
/*
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
/*
import java.applet.Applet;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/**
 * @author Sergey A. Malenkov
 */
/*
public class triangle extends Applet implements MouseMotionListener {

    private static final double S = 40.0;
    private static final double H = S * 1.5;
    private static final double R = S * Math.cos(Math.PI / 6.0);
    private static final double W = R * 2.0;

    private int i;
    private int j;

    private final int[] x = new int[3];
    private final int[] y = new int[this.x.length];

    { addMouseMotionListener(this); }

    @Override
    public void paint(Graphics g) {
        FontMetrics fm = g.getFontMetrics();

        double width = R + (double) getWidth();
        double height = H + (double) getHeight();

        int i = 0;
        int j = 0;

        double y = 0.0;
        while (y < height) {

            double x = (double) i * R - R * (double) j;
            while (x < width) {
                boolean up = (i & 1) == 1;

                this.x[0] = (int) (x - R);
                this.y[0] = (int) (up ? y : y + H);

                this.x[1] = (int) (x);
                this.y[1] = (int) (up ? y + H : y);

                this.x[2] = (int) (x + R);
                this.y[2] = this.y[0];

                double distance = 0.0;
                if ((this.i != i) || (this.j != j)) {
                    int di = 2 * Math.abs(this.i / 2 - i / 2);
                    int dj = 2 * Math.abs(this.j - j);
                    boolean flag = ((this.i <= i) && (this.j >= j)) || ((this.i >= i) && (this.j <= j));
                    distance = S * (double) (flag ? di + dj : Math.max(di, dj));
                    if ((this.i & 1) != (i & 1)) {
                        distance += (up == (2 * (this.j - j) < (this.i - i)))
                                    ? -S
                                    : S;
                    }
                }
                boolean fill = distance <= W * 8;
                if (fill) {
                    float c = (float) (distance / W / 10.0);
                    g.setColor(new Color(c, c, c));
                    g.fillPolygon(this.x, this.y, this.y.length);
                    g.setColor(getBackground());
                }
                else {
                    g.drawPolygon(this.x, this.y, this.y.length);
                }

                String s1 = i + " ; " + j;
                String s2 = Integer.toString((int) distance);

                int sy = (int) (y + (up ? H - S : S)) - fm.getDescent();
                g.drawString(s1, (int) (x - (double) fm.stringWidth(s1) / 2.0), sy);
                g.drawString(s2, (int) (x - (double) fm.stringWidth(s2) / 2.0), sy + fm.getHeight());

                if (fill) {
                    g.setColor(getForeground());
                }
                x += R;
                i++;
            }
            y += H;
            j++;
            i = j - 1;
        }
    }

    public void mouseDragged(MouseEvent event) {
        mouseMoved(event);
    }

    public void mouseMoved(MouseEvent event) {
        double y = (double) event.getY();
        int j = (int) Math.floor(y / H);

        double x = (double) event.getX() + (double) j * R + R;
        int i = (int) Math.floor(x / R);

        y -= (double) j * H;
        if ((i & 1) != 1) {
            y = H - y;
        }
        x -= (double) i * R;
        x *= H / R;
        if (y > x) {
            i--;
        }
        if ((this.i != i) || (this.j != j)) {
            this.i = i;
            this.j = j;
            repaint();
        }
    }
}*/